package com.ponhu.ea2.goodsCenter.provider.utils;

import com.alibaba.fastjson.JSONObject;
import com.ponhu.ea2.entity.SyncAiDataLogs;
import com.ponhu.ea2.enums.SyncAiDataStatusEnum;
import com.ponhu.ea2.enums.SyncAiDataTypeEnum;
import com.ponhu.ea2.goodsCenter.provider.service.SyncAiDataLogsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@RefreshScope
public class BaoxjUtils {

    @Value("${third-party.turingsenseai.classifyUrl}")
    private String classifyUrl;

    @Value("${third-party.turingsenseai.openPlatformApiKey}")
    private String openPlatformApiKey;

    @Value("${third-party.turingsenseai.openPlatformApiSecret}")
    private String openPlatformApiSecret;

    @Resource
    private SyncAiDataLogsService syncAiDataLogsService;

    public String aiPictureClassify(String imgUrl) {
        if (StringUtils.isEmpty(imgUrl)) return null;

        String apiResponse = null;
        SyncAiDataStatusEnum syncAiDataStatusEnum = null;
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            headers.add("Open-Platform-Api-Key", openPlatformApiKey);
            headers.add("Open-Platform-Api-Secret", openPlatformApiSecret);

            Map<String, Object> param = new HashMap<>();
            param.put("image_url", imgUrl);
            HttpEntity<Map> httpEntity = new HttpEntity<>(param, headers);

            JSONObject aiPictureObj = new RestTemplate().postForObject(classifyUrl, httpEntity, JSONObject.class);

            apiResponse = aiPictureObj.toJSONString();
            if (!aiPictureObj.getString("error_msg").equals("Success"))
                syncAiDataStatusEnum = SyncAiDataStatusEnum.ERROR;
            else syncAiDataStatusEnum = aiPictureObj.getBoolean("has_match")
                    ? SyncAiDataStatusEnum.SUCCESS : SyncAiDataStatusEnum.FAIL;

            return aiPictureObj.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            apiResponse = "程序异常";
            syncAiDataStatusEnum = SyncAiDataStatusEnum.ERROR;
            log.error("包小检AI识图异常 ===>> imgUrl={} ===>> ERROR={}", imgUrl, e.getMessage());
            return null;
        } finally {
            SyncAiDataLogs syncAiDataLogs = new SyncAiDataLogs()
                    .setImgUrl(imgUrl)
                    .setApiResponse(apiResponse)
                    .setStatus(syncAiDataStatusEnum.toString())
                    .setType(SyncAiDataTypeEnum.BAO_XIAO_JIAO.getCode());
            syncAiDataLogsService.save(syncAiDataLogs);
        }
    }

}
